package com.oracle.children;

import android.app.DatePickerDialog;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.RadioButton;
import android.widget.Spinner;

import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lws.basic.model.OkHttpUtils;

import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.utils.common.SharePreferenceUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

//新生儿家庭访视记录表
public class ChildrenArchivesActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private ArchivesExtendableListAdapter archivesExtendableListAdapter;
    private Map<String, Object> mValues = new HashMap<>();
    private int mJmid;
    private String mUrl;
    private Bean mBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.childrenadd_archives_activity);
        // 启用Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        expandableListView = findViewById(R.id.expend_list1);
        String[] archivesStr = getResources().getStringArray(R.array.children_archives);
        archivesExtendableListAdapter = new ArchivesExtendableListAdapter(archivesStr);
        expandableListView.setAdapter(archivesExtendableListAdapter);
        // 添加末尾提交按钮
        View foot = LayoutInflater.from(this).inflate(R.layout.childrenarchives_foot_submit, null);
        expandableListView.addFooterView(foot);


        mJmid = getIntent().getIntExtra("jmid", 0);
        mValues.put("jmId", mJmid);
        mValues.put("userId", SharePreferenceUtils.getUserId(this));//社区医生id;
        mValues.put("token", SharePreferenceUtils.getToken(this));
        mValues.put("mark", 1);
        //随访id
        int id = getIntent().getIntExtra("id", 0);
        if (id != 0) {
            mValues.put("sfjlId", id);
            mValues.put("id", id);
        }

        mUrl = SharePreferenceUtils.getString(this, "base_url", "");
        MyLogger.i(mUrl);
        final Map<Object, Object> values = new HashMap<>();
        values.put("mark", 1);
        values.put("token", SharePreferenceUtils.getToken(this));
        values.put("grjbxxId", mJmid);
        final JSONObject jsonObject = new JSONObject(values);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {

                return false;
            }
        });
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                int count = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < count; i++) {
                    if (i != groupPosition) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });



        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                View[] views = ((ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
                View view = views[groupPosition];
                if (view == null) {
                    return;
                }
                Map<String, String> values = null;
                switch (groupPosition) {
                    case 0:
                        values = essentialInformation();
                        break;
                    case 1:
                        values = bothParentsNames(view);
                        break;
                    case 2:
                        values = birthInformation(view);
                        break;
                    case 3:
                        values = healthyNewborn(view);
                        break;
                    case 4:
                        values = physicalExaminationOfNewbornSurgery(view);
                        break;
                    case 5:
                        values = physicalExaminationOfNewbornInternal(view);
                        break;
                    case 6:
                        values = adviceAndGuidance(view);
                        break;
                    case 7:
                        values = dateOfExamination(view);
                        break;
                }



            }
        });

        expandableListView.expandGroup(0);
        expandableListView.expandGroup(1);

        OkGo.<String>post(mUrl + "online/xse/checkFM")
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();
                        Gson gson = new Gson();
                        mBean = gson.fromJson(body, Bean.class);
                        MyLogger.i(body);
                        View[] views = ((ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
                        View view = null;
                        view = views[1];
                        if (null != view) {
                            if (mBean != null && mBean.getData() != null) {
                                mValues.put("jbxxXseId", mBean.getData().getId());
                                ((EditText) view.findViewById(R.id.archives_fathername)).setText(mBean.getData().getFxm());
                                ((EditText) view.findViewById(R.id.archives_professional1)).setText(mBean.getData().getFzy());
                                ((EditText) view.findViewById(R.id.archives_phonenumber1)).setText(mBean.getData().getFdh());
                                ((TextView) view.findViewById(R.id.archives_birthday1)).setText(mBean.getData().getFcsrq());
                                ((EditText) view.findViewById(R.id.archives_mothername)).setText(mBean.getData().getMxm());
                                ((EditText) view.findViewById(R.id.archives_professional2)).setText(mBean.getData().getMzy());
                                ((EditText) view.findViewById(R.id.archives_phonenumber2)).setText(mBean.getData().getMdh());
                                ((TextView) view.findViewById(R.id.archives_birthday2)).setText(mBean.getData().getMcsrq());
                            }
                        }
                    }
                });



    }

    /**
     * Toolbar的事件---返回
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 点击日期选择框弹出DatePickerDialog
     */
    private Calendar calendar;
    private DatePickerDialog dialog;

    public void onTimeViewClick(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(ChildrenArchivesActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        String month;
                        String day;
                        if (monthOfYear <9){
                            month = "0"+(monthOfYear+1);
                        }else {
                            month = ""+(monthOfYear+1);
                        }
                        if (dayOfMonth <10){
                            day = "0"+dayOfMonth;
                        }else {
                            day = dayOfMonth + "";
                        }
                        System.out.println("后"+year + "-" + month  + "-" + day);
                        tv.setText(year + "-" + month  + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
    /**
     * "转诊建议"按钮点击事件处理
     */
    public void onEdit(View view) {
        EditText ed1 = findViewById(R.id.archives_reason);
        EditText ed2 = findViewById(R.id.archives_institutions);
        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.archives_none) {
            ed1.setText("");
            ed1.setEnabled(false);
            ed2.setText("");
            ed2.setEnabled(false);
        } else if (rb.isChecked()) {
            ed1.setEnabled(true);
            ed2.setEnabled(true);
        }
    }

    /**
     * "面色"按钮点击事件处理
     */
    public void dianji(View view) {
        EditText ed = findViewById(R.id.archives_enter_name);
        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.archives_ruddy || view.getId() == R.id.archives_yellow) {
            ed.setText("");
            ed.setEnabled(false);
        } else if (rb.isChecked()) {
            ed.setEnabled(true);
        }
    }

    /**
     * 检查用户名、身份证号和本人联系电话是否正确填写
     *
     * @return 数据不正确返回true，正确返回false
     */
    private boolean checkBasicInfoIsError() {
        Map<String, String> basicInforValues = essentialInformation();
        String xm = basicInforValues.get("xm");
        String sfzh = basicInforValues.get("sfz");
        String brdh = basicInforValues.get("brdh");
        // 姓名非空验证
        if (xm == null || xm.length() == 0) {
            Toast.makeText(getApplicationContext(), "姓名必须填写", Toast.LENGTH_SHORT).show();
            return true;
        }
        // 身份证非空验证
        if (sfzh == null || sfzh.length() == 0) {
            Toast.makeText(getApplicationContext(), "身份证号必须填写", Toast.LENGTH_SHORT).show();
            return true;
        }
        // 验证身份证号格式
        String idCardRegex = "^([1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx])|([1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2})$";
        if (!sfzh.matches(idCardRegex)) {
            Toast.makeText(getApplicationContext(), "身份证号格式不正确", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }


    /**
     * 获取页面数据
     *
     * @return 封装好的数据
     */
    /*
     * 第一页，基本信息
     */
    private Map<String, String> essentialInformation(){
        View[] views = ((ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
        View view = views[0];
        Map<String, String> params = new HashMap<>();
        // 姓名
        String username = "";
        // 编号
        String number = "";
        // 性别
        String gender = "";
        // 出生日期
        String birthday = "";
        // 家庭住址
        String Homeaddress = "";
        //身份证号
        String Idcard = "";
        username = ((EditText) view.findViewById(R.id.archives_username)).getText().toString().trim();
        gender = ((Spinner) view.findViewById(R.id.archives_gender)).getSelectedItem().toString().trim();
        gender = gender.substring(0, gender.indexOf(" "));
        birthday = ((TextView) view.findViewById(R.id.archives_birthday)).getText().toString().trim();
        Homeaddress = ((EditText) view.findViewById(R.id.archives_Homeaddress)).getText().toString().trim();
        Idcard = ((EditText) view.findViewById(R.id.archives_Idcard)).getText().toString().trim();
        params.put("xm", username); //姓名
        params.put("xb", gender);// 性别
        params.put("csrq", birthday);//出生日期
        params.put("jtzz", Homeaddress);//住址
        params.put("sfz", Idcard);//身份证号
        mValues.putAll(params);
        return params;
    }
    /*
     * 第二页，双亲姓名
     */
    private Map<String, String> bothParentsNames(View view) {
        Map<String, String> params = new HashMap<>();
        //父亲姓名
        String fathername = "";
        // 职业
        String professional1 = "";
        // 联系电话
        String phonenumber1 = "";
        // 出生日期
        String birthday1 = "";
        //母亲姓名
        String mothername = "";
        // 职业
        String professional2 = "";
        // 联系电话
        String phonenumber2 = "";
        // 出生日期
        String birthday2 = "";
       /* if (mBean != null &&mBean.getData() != null){
            MyLogger.i("双亲信息");
            ((EditText) view.findViewById(R.id.archives_fathername)).setText(mBean.getData().getFxm());
            ((EditText) view.findViewById(R.id.archives_professional1)).setText(mBean.getData().getFzy());
            ((EditText) view.findViewById(R.id.archives_phonenumber1)).setText(mBean.getData().getFdh());
            ((TextView) view.findViewById(R.id.archives_birthday1)).setText(mBean.getData().getFcsrq());
            ((EditText) view.findViewById(R.id.archives_mothername)).setText(mBean.getData().getMxm());
            ((EditText) view.findViewById(R.id.archives_professional2)).setText(mBean.getData().getMzy());
            ((EditText) view.findViewById(R.id.archives_phonenumber2)).setText(mBean.getData().getMdh());
            ((TextView) view.findViewById(R.id.archives_birthday2)).setText(mBean.getData().getMcsrq());
        }*/
        fathername = ((EditText) view.findViewById(R.id.archives_fathername)).getText().toString().trim();
        professional1 = ((EditText) view.findViewById(R.id.archives_professional1)).getText().toString().trim();
        phonenumber1 = ((EditText) view.findViewById(R.id.archives_phonenumber1)).getText().toString().trim();
        birthday1 = ((TextView) view.findViewById(R.id.archives_birthday1)).getText().toString().trim();
        mothername = ((EditText) view.findViewById(R.id.archives_mothername)).getText().toString().trim();
        professional2 = ((EditText) view.findViewById(R.id.archives_professional2)).getText().toString().trim();
        phonenumber2 = ((EditText) view.findViewById(R.id.archives_phonenumber2)).getText().toString().trim();
        birthday2 = ((TextView) view.findViewById(R.id.archives_birthday2)).getText().toString().trim();

        params.put("fxm", fathername);//父亲姓名
        params.put("fzy", professional1);//父亲职业
        params.put("fdh", phonenumber1);//父亲电话
        params.put("fcsrq", birthday1);//父亲出生日期
        params.put("mxm", mothername);// 母亲姓名
        params.put("mzy", professional2);//职业
        params.put("mdh", phonenumber2);//电话
        params.put("mcsrq", birthday2);//出生
        mValues.putAll(params);
        return params;
    }

    // 第三页，出生信息
    private Map<String, String> birthInformation(View view) {
        Map<String, String> params = new HashMap<>();

        //出生孕周
        String week="";
        //母亲妊娠期患病情况
        String gestation="";
        //助产机构名称
        String name="";
        //出生情况
        String birth="";
        //新生儿窒息
        String stifle="";
        //Apgar评分
        String grade="";
        //畸型
        String dysplastic="";
        week = ((EditText) view.findViewById(R.id.archives_week)).getText().toString().trim();
        gestation = ((Spinner) view.findViewById(R.id.archives_gestation)).getSelectedItem().toString().trim();
        gestation = gestation.substring(0,gestation.indexOf(" "));
        name = ((EditText) view.findViewById(R.id.archives_name)).getText().toString().trim();
        birth = ((Spinner) view.findViewById(R.id.archives_birth)).getSelectedItem().toString().trim();
        birth = birth.substring(0,birth.indexOf(" "));
        stifle = ((Spinner) view.findViewById(R.id.archives_stifle)).getSelectedItem().toString().trim();
        stifle = stifle.substring(0,stifle.indexOf(" "));
        grade = ((EditText) view.findViewById(R.id.archives_grade)).getText().toString().trim();
        dysplastic = ((Spinner) view.findViewById(R.id.archives_dysplastic)).getSelectedItem().toString().trim();
        dysplastic = dysplastic.substring(0,dysplastic.indexOf(" "));
        params.put("csyz", week);//出生孕周
        params.put("rcqhbqk", gestation);// //母亲妊娠期患病情况
        params.put("zcjgmc", name);// //助产机构名称
        params.put("csqk", birth);//出生情况
        params.put("xseZx", stifle);//新生儿窒息
        params.put("aqf", grade);////Apgar评分
        params.put("xseJx", dysplastic);//畸型
        mValues.putAll(params);
        return params;
    }
    /**
     * 第四页，新生儿身体健康
     */
    private Map<String, String> healthyNewborn(View view) {
        Map<String, String> params = new HashMap<>();

        // 新生儿听力筛查
        String hearing="";
        // 新生儿疾病筛查
        String disease1="";
        // 新生儿出生体重
        String weight1="";
        // 目前体重
        String weight2="";
        // 出生身长
        String length="";
        // 喂养方式
        String feeding="";
        // 吃奶量
        String Eatmilk1="";
        // 吃奶次数
        String milknumber="";
        // 呕吐
        String vomiting="";
        // 大便
        String movebowels1="";
        // 大便次数
        String dabiancishu="";
        // 体温
        String temperature="";
        // 心率
        String Heartrate="";
        // 呼吸频率
        String Breathingrate="";
        hearing = ((Spinner) view.findViewById(R.id.archives_hearing)).getSelectedItem().toString().trim();
        hearing = hearing.substring(0,hearing.indexOf(" "));
        disease1 = ((Spinner) view.findViewById(R.id.archives_disease1)).getSelectedItem().toString().trim();
        disease1 = disease1.substring(0, disease1.indexOf(" "));
        weight1 = ((EditText) view.findViewById(R.id.archives_weight1)).getText().toString().trim();
        weight2= ((EditText) view.findViewById(R.id.archives_weight2)).getText().toString().trim();
        length = ((EditText) view.findViewById(R.id.archives_length)).getText().toString().trim();
        feeding = ((Spinner) view.findViewById(R.id.archives_feeding)).getSelectedItem().toString().trim();
        feeding= feeding.substring(0, feeding.indexOf(" "));
        Eatmilk1 = ((EditText) view.findViewById(R.id.archives_Eatmilk1)).getText().toString().trim();
        milknumber = ((EditText) view.findViewById(R.id.archives_milknumber)).getText().toString().trim();
        vomiting = ((Spinner) view.findViewById(R.id.archives_vomiting)).getSelectedItem().toString().trim();
        vomiting = vomiting.substring(0, vomiting.indexOf(" "));
        movebowels1 = ((Spinner) view.findViewById(R.id.archives_movebowels1)).getSelectedItem().toString().trim();
        movebowels1 = movebowels1.substring(0, movebowels1.indexOf(" "));
        dabiancishu = ((EditText) view.findViewById(R.id.archives_dabian2)).getText().toString().trim();
        temperature = ((EditText) view.findViewById(R.id.archives_temperature)).getText().toString().trim();
        Heartrate= ((EditText) view.findViewById(R.id.archives_Heartrate)).getText().toString().trim();
        Breathingrate = ((EditText) view.findViewById(R.id.archives_Breathingrate)).getText().toString().trim();
        params.put("xseTlsc", hearing);// 新生儿听力筛查
        params.put("xseJbsc", disease1);// 新生儿疾病筛查
        params.put("xseCstz", weight1);  // 新生儿出生体重
        params.put("xseMqtz",weight2); // 目前体重
        params.put("xseCssz", length);  //出生身长
        params.put("xseWyfs",feeding);   // 喂养方式
        params.put("xseCnl", Eatmilk1); // //新生儿吃奶量mL/次
        params.put("xseCncs", milknumber);//新生儿吃奶次数
        params.put("xseOt", vomiting);//呕吐
        params.put("xseDb", movebowels1);// 大便
        params.put("xseDbcs", dabiancishu); //大便次数
        params.put("xseTw", temperature);//新生儿体温
        params.put("xseXl", Heartrate);    //心率
        params.put("xseHxpl", Breathingrate);//新生儿呼吸频率次
        mValues.putAll(params);
        return params;
    }

    //第五页，新生儿身体检查（外科）
    private Map<String, String> physicalExaminationOfNewbornSurgery(View view) {
        Map<String, String> params = new HashMap<>();

        //面色
        String complexion="";
        String complexionOther="";
        //黄疸部位
        String Jaundice_parts="";
        //前囟
        String bregma="";
        String kong="";
        //眼睛
        String eye="";
        //四肢活动度
        String limb_mobility="";
        //耳外观
        String ear="";
        //颈部包块
        String cervical_mass="";
        //鼻
        String nose="";
        //皮肤
        String skin="";

        if (view != null) {
            RadioButton ruddy = view.findViewById(R.id.archives_ruddy);
            RadioButton yellow = view.findViewById(R.id.archives_yellow);
            RadioButton other = view.findViewById(R.id.archives_other);
            if (ruddy.isChecked()) {
                String s = ruddy.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                complexion = s;
            } else if(yellow.isChecked()){
                String s = yellow.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                complexion = s;
            }else {
                String s = yellow.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                complexion = s;
            }
            complexionOther = ((EditText) view.findViewById(R.id.archives_enter_name)).getText().toString().trim();
        }
        Jaundice_parts = ((Spinner) view.findViewById(R.id.archives_Jaundice_parts)).getSelectedItem().toString().trim();
        Jaundice_parts = Jaundice_parts.substring(0, Jaundice_parts.indexOf(" "));
       // bregma = ((EditText) view.findViewById(R.id.archives_bregma1)).getText().toString().trim();
        MyLogger.i(bregma+"前囟");
       // bregma = bregma.substring(0, bregma.indexOf(" "));
        kong = ((EditText) view.findViewById(R.id.archives_bregma2)).getText().toString().trim();
        eye = ((Spinner) view.findViewById(R.id.archives_eye)).getSelectedItem().toString().trim();
        eye = eye.substring(0, eye.indexOf(" "));
        limb_mobility = ((Spinner) view.findViewById(R.id.archives_limb_mobility)).getSelectedItem().toString().trim();
        limb_mobility = limb_mobility.substring(0, limb_mobility.indexOf(" "));
        ear = ((Spinner) view.findViewById(R.id.archives_ear)).getSelectedItem().toString().trim();
        ear = ear.substring(0, ear.indexOf(" "));
        cervical_mass = ((Spinner) view.findViewById(R.id.archives_cervical_mass)).getSelectedItem().toString().trim();
        cervical_mass = cervical_mass.substring(0, cervical_mass.indexOf(" "));
        nose = ((Spinner) view.findViewById(R.id.archives_nose)).getSelectedItem().toString().trim();
        nose = nose.substring(0, nose.indexOf(" "));
        skin = ((Spinner) view.findViewById(R.id.archives_skin)).getSelectedItem().toString().trim();
        skin = skin.substring(0, skin.indexOf(" "));
        bregma = ((Spinner) view.findViewById(R.id.archives_bregma)).getSelectedItem().toString().trim();
        bregma = bregma.substring(0, bregma.indexOf(" "));
        params.put("xseMs", complexion); //面色
        params.put("msqt", complexionOther);//面部其他
        params.put("xseHdbw", Jaundice_parts); //黄疸部位
        params.put("xseQl", bregma); //前囟
       // params.put("bre", kong);  //空保留
        params.put("xseYj", eye);//眼睛
        params.put("xseSzhdd", limb_mobility);//四肢活动度
        params.put("xseEwg", ear);  //尔外观
        params.put("xseJbbk", cervical_mass);////颈部包块
        params.put("xseB", nose);//鼻子
        params.put("xsePf", skin);//皮肤
        mValues.putAll(params);
        return params;
    }

    /*
     *第六页，新生儿身体检查（内部）
     */
    private Map<String, String> physicalExaminationOfNewbornInternal(View view) {
        Map<String, String> params = new HashMap<>();

        // 口腔
        String mouth="";
        // 肛门
        String anus="";
        // 心肺听诊
        String lung_auscultation="";
        // 胸部
        String chest="";
        // 腹部触诊
        String abdominal_touch="";
        // 脊柱
        String backbone="";
        // 外生殖器
        String pudendum="";
        //脐带
        String funicle="";
        mouth = ((Spinner) view.findViewById(R.id.archives_mouth)).getSelectedItem().toString().trim();
        mouth = mouth.substring(0, mouth.indexOf(" "));
        anus = ((Spinner) view.findViewById(R.id.archives_anus)).getSelectedItem().toString().trim();
        anus = anus.substring(0, anus.indexOf(" "));
        lung_auscultation = ((Spinner) view.findViewById(R.id.archives_Lung_auscultation)).getSelectedItem().toString().trim();
        lung_auscultation = lung_auscultation.substring(0, lung_auscultation.indexOf(" "));
        chest = ((Spinner) view.findViewById(R.id.archives_chest)).getSelectedItem().toString().trim();
        chest = chest.substring(0, chest.indexOf(" "));
        abdominal_touch = ((Spinner) view.findViewById(R.id.archives_abdominal_touch)).getSelectedItem().toString().trim();
        abdominal_touch = abdominal_touch.substring(0, abdominal_touch.indexOf(" "));
        backbone = ((Spinner) view.findViewById(R.id.archives_backbone)).getSelectedItem().toString().trim();
        backbone = backbone.substring(0, backbone.indexOf(" "));
        pudendum = ((Spinner) view.findViewById(R.id.archives_pudendum)).getSelectedItem().toString().trim();
        pudendum = pudendum.substring(0, pudendum.indexOf(" "));
        funicle = ((Spinner) view.findViewById(R.id.archives_funicle)).getSelectedItem().toString().trim();
        funicle = funicle.substring(0, funicle.indexOf(" "));
        params.put("xseKq",mouth);//口腔
        params.put("xseGm",anus);//肛门
        params.put("xseXb",chest);//胸部
        params.put("xseXftz",lung_auscultation);//心肺听诊
        params.put("xseFbcz",abdominal_touch);//    // 腹部触诊
        params.put("xseJz",backbone);//// 脊柱
        params.put("xseWszq",pudendum);//外生殖器
        params.put("xseQd",funicle);//脐带
        mValues.putAll(params);//
        return params;
    }

    /**
     * 第七页，建议及指导
     */
    private Map<String, String> adviceAndGuidance(View view) {
        Map<String, String> params = new HashMap<>();

        // 转诊建议
        String Referraladvice="";
        String Referraladvicename="";
        //机构及科室
        String institutions="";
        // 指导
        String guide="1";
        guide = ((Spinner) view.findViewById(R.id.archives_guide)).getSelectedItem().toString().trim();
        guide= guide.substring(0, guide.indexOf(" "));
        MyLogger.i(guide+"指导");
        if (view != null) {
            RadioButton none = view.findViewById(R.id.archives_none);
            RadioButton all = view.findViewById(R.id.archives_genetic_disease_all);
            if (none.isChecked()) {
                String s = none.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                Referraladvice = s;
            } else {
                String s = all.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                Referraladvice = s;
            }
            Referraladvicename = ((EditText) view.findViewById(R.id.archives_reason)).getText().toString().trim();
            institutions = ((EditText) view.findViewById(R.id.archives_institutions)).getText().toString().trim();
        }
        params.put("zzjy", Referraladvice);    // 转诊建议
        params.put("zzyx", Referraladvicename);//原因
        params.put("zzjgjks", institutions);    //机构及科室
        params.put("zd", guide); //// 指导
        mValues.putAll(params);
        return params;
    }

    //第八页，检查日期
    private Map<String, String> dateOfExamination(View view) {
        Map<String, String> params = new HashMap<>();

        //本次访视日期
        String birthday3="";
        //下次随访日期
        String birthday4="";
        //下次随访地点
        String site="";
        //随访医生签名
        String signature="";
        birthday3 = ((TextView) view.findViewById(R.id.archives_birthday3)).getText().toString().trim();
        birthday4 = ((TextView) view.findViewById(R.id.archives_birthday4)).getText().toString().trim();
        site = ((EditText) view.findViewById(R.id.archives_site)).getText().toString().trim();
        signature = ((EditText) view.findViewById(R.id.archives_signature)).getText().toString().trim();
        params.put("bcsfrq", birthday3);
        params.put("xcsfrq", birthday4);//下次随访
        params.put("xcsfdd", site);    //随访地点
        params.put("sfysqm", signature); //医生签名
        mValues.putAll(params);
        return params;
    }


    /**
     * 获取子视图
     */
    private View[] views;

    private View[] getViews(View view) {
        views = new View[0];
        parseView(view);
        return views;
    }

    private void parseView(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) view;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View v = vg.getChildAt(i);
                parseView(v);
            }
        } else {
            views = Arrays.copyOf(views, views.length + 1);
            views[views.length - 1] = view;
        }
    }


    /**
     * 提交数据
     */
    public void onClickSubmit(View view) {
        int count = expandableListView.getExpandableListAdapter().getGroupCount();
        for (int i = 0; i < count; i++) {
            expandableListView.collapseGroup(i);
        }
        JSONObject params = new JSONObject(mValues);
        System.out.println(params.toString());

        System.out.println("新生儿： "+params.toString());

        MyLogger.i(mUrl);
        OkGo.<String>post( mUrl+"online/xse/save")
            .upJson(params.toString())
            .execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    String body = response.body();
                    MyLogger.i(body);
                    Gson gson = new Gson();
                    DataBean dataBean = gson.fromJson(body, DataBean.class);
                    if(dataBean.getCode()==0){
                        Toast.makeText(ChildrenArchivesActivity.this,"提交成功",Toast.LENGTH_LONG).show();
                        finish();
                    }else {
                        Toast.makeText(ChildrenArchivesActivity.this,"提交成功",Toast.LENGTH_LONG).show();
                    }
                    MyLogger.i(body);
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    MyLogger.i(response.body());
                }

            });

    }

}
